import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { getAddress } from '@/api/print/address.js'
import { getLocation, setLocation, getChunk, setChunk } from '@/utils/systemTools'

const baseUrl = config.baseUrl

const system = {
  state: {
    location: getLocation(),
		uploadedChunkList: getChunk()||[],
		uploadedFiles: uni.getStorageSync('uploadedFiles'),
		addressInfo: null,
  },

  mutations: {
    SET_LOCATION: (state, location) => {
      state.location = location;
			setLocation(location);
    },
		ADD_CHUNK: (state, chunksValue) => {
		  state.uploadedChunkList.push(...chunksValue);
			setChunk(state.uploadedChunkList);
		},
		DELETE_CHUNK: (state, chunksValue) => {
			for(var i = 0; i < chunksValue.length; i++) {
				let index = state.uploadedChunkList.indexOf(chunksValue[i])
				if( -1 != index) {
					state.uploadedChunkList.slice(index,1);
				}
			} 
			setChunk(state.uploadedChunkList);
		},
    SET_UPLOADEDFILE: (state, uploadedFiles) => {
      state.uploadedFiles = uploadedFiles;
			uni.setStorageSync('uploadedFiles',uploadedFiles);
    },
    SET_ADDRESS: (state, addressInfo) => {
      state.addressInfo = addressInfo;
    },
  },

  actions: {
    // 获取最近地址
    getLocal(latitude,longitude,name) {
      name = name.trim() 
      return new Promise((resolve, reject) => {
        getAddress(latitude,longitude,name).then(res => {					
          setLocation(res.rows[0])
					commit('SET_LOCATION', res.rows[0]) 
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  },
}

export default system
